package acwing.unkonw;

import java.util.Arrays;

import leetcode.helper.HelpDebug;

public class Solution {

	int[] t;

	public int solution(int c, int[] times) {
		int len = times.length;
		this.t = times;
		Arrays.sort(t);
		Integer[][][] dp = new Integer[(len - 1) * 3 + t[len - 1] + 1][len + 1][len];
		return this.find(0, 1, len - 1, dp);
	}

	private int find(int time, int vmsize, int k, Integer[][][] dp) {
		if (vmsize >= k + 1) {
			return time + t[k];
		}
		if (dp[time][vmsize][k] != null) {
			return dp[time][vmsize][k];
		}
		int ans1 = time + t[k];
		int ans2 = Integer.MAX_VALUE;
		for (int i = 0; i < vmsize; i++) {
			ans2 = Math.min(ans2, this.find(time + 3, (vmsize - i) << 1, k - i, dp));
		}
		int ans = Math.max(ans1, ans2);
		dp[time][vmsize][k] = ans;
		return ans;
	}

	public static void main(String[] args) {
		Solution so = new Solution();
		HelpDebug.compare(12, so.solution(3, HelpDebug.str2array3("[1,8,4,3]")));
		HelpDebug.compare(10, so.solution(3, HelpDebug.str2array3("[5,1,1,1,1]")));
	}

}
